package yuyicman.utils 
{
	import com.klstudio.display.IconDataManager;
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.ui.Mouse;
	
	/**
	 * ...
	 * @author Yuyic.
	 */
	public class MouseShape extends Sprite
	{
		
		public static const maximize:String = "maximize";
		public static const minimize:String = "minimize";
		public static const deoxidize:String = "deoxidize";
		public static const close:String = "close";
		public static const resize:String = "resize";
		public static const prev:String = "prev";
		public static const next:String = "next";
		public static const first:String = "first";
		public static const end:String = "end";
		public static const miniArrow:String = "miniArrow";
		public static const arrow:String = "arrow";
		public static const cursor_stop:String = "cursor_stop";
		public static const cursor_white_arrow:String = "cursor_white_arrow";
		public static const cursor_black_arrow:String = "cursor_black_arrow";
		public static const cursor_cut:String = "cursor_cut";
		public static const cursor_pen:String = "cursor_pen";
		public static const cursor_paint:String = "cursor_paint";
		public static const cursor_scale_small:String = "cursor_scale_small";
		public static const cursor_scale_big:String = "cursor_scale_big";
		public static const cursor_hand:String = "cursor_hand";
		public static const cursor_hand_down:String = "cursor_hand_down";
		public static const cursor_box_move:String = "cursor_box_move";
		public static const cursor_control_move:String = "cursor_control_move";
		public static const cursor_control_positive:String = "cursor_control_positive";
		public static const cursor_control_oblique:String = "cursor_control_oblique";
		public static const origin:String = "origin";
		
		private static var _ins:MouseShape;
		private var _bmp:Bitmap;
		private var _style:String = "origin";
		
		public function MouseShape() 
		{
			addEventListener(Event.ADDED_TO_STAGE, _init);
		}
		
		private function _init( e:Event ):void {
			removeEventListener(Event.ADDED_TO_STAGE, _init);
		}
		
		private function _mouseMoveHandler( e:MouseEvent ):void {
			position();
			e.updateAfterEvent();
		}
		
		private function position():void {
			x = stage.mouseX;
			y = stage.mouseY;
			if ( parent ) {
				parent.setChildIndex( this, parent.numChildren - 1 );
			}
		}
		
		public function styleTo( name:String ):void {
			
			stage.addEventListener(MouseEvent.MOUSE_MOVE, _mouseMoveHandler );
			
			if ( _style == name ) {
				return;
			}
			
			if ( _bmp ) {
				removeChild( _bmp );
			}
			_bmp = addChild( IconDataManager.getIcon( name ) ) as Bitmap;
			position();
			Mouse.hide();
			
		}
		
		public function restore():void {
			
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, _mouseMoveHandler);
			_style = origin;
			Mouse.show();
			if ( _bmp ) {
				removeChild( _bmp );
				_bmp = null;
			}
			
		}
		
		public static function restore():void {
			instance.restore();
		}
		
		public static function styleTo( name:String ):void {
			instance.styleTo( name );
		}
		
		public static function get instance():MouseShape {
			if ( !_ins ) {
				_ins = new MouseShape;
			}
			return _ins;
		}
		
	}

}